package com.day12_Thread.ExcutorServiceDemo;

import java.util.concurrent.*;

/**
 * @ClassName: ExcutorServiceDemo1
 * @Author: zw
 * @Description: 出百万拳，行万里路；
 * @Create 2025/4/6-11:14
 * @Version 1.0
 */
public class ExcutorServiceDemo1 {
    // 处理Callable任务
    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 5, 5, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3), Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy());
        // 1. 处理Callable 任务
        Future<String> f1 = threadPoolExecutor.submit(new MyCallable(4));
        Future<String> f2 = threadPoolExecutor.submit(new MyCallable(5));
        Future<String> f3 = threadPoolExecutor.submit(new MyCallable(6));
        Future<String> f4 = threadPoolExecutor.submit(new MyCallable(7));
        try {
            System.out.println(f1.get());
            System.out.println(f2.get());
            System.out.println(f3.get());
            System.out.println(f4.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
class MyCallable implements Callable<String> {
    private int n;
    public MyCallable(int n) {
        this.n = n;
    }
    @Override
    public String call() throws Exception {
        int sum = 0;
        String name = Thread.currentThread().getName();
        for (int i = 0; i < n; i++) {
            sum+=i;
        }
        return name + ":子线程输出：" + sum;
    }
}
